package com.java.servlet.web.mail.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.java.servlet.web.commons.PropertiesUtils;
import com.java.servlet.web.mail.service.MailService;
import com.java.servlet.web.mail.service.MailServiceImpl;
import com.java.servlet.web.mail.vo.MailVo;

@WebServlet (name = "MailController", urlPatterns = "/mail/mailController")
public class MailController extends HttpServlet {
	private static final long serialVersionUID = 6451090012522852262L;
	private static final Logger log = Logger.getLogger(MailController.class);
	private static final int INIT_MAIL = 0;
	private static final int SEND_MAIL = 1;
	private static final int VALIDAR_REGS = 2;
	private Properties viewConfig;
	private Properties fieldConfig;
	private MailService mailService;
	
	public MailController(){
	}
    public void init() {
		try {
			super.init();
			mailService = new MailServiceImpl();
			viewConfig = PropertiesUtils.getPropertiesFromFile(getServletContext().getResource("/WEB-INF/view-config.properties"));
			fieldConfig = PropertiesUtils.getPropertiesFromFile(getServletContext().getResource("/WEB-INF/form-config.properties"));
		} catch (MalformedURLException ex) {
			log.info("Error fatal, no se puedo cargar el archivo de configuracion de vistas." + ex);
		} catch(ServletException serEx){
			log.info("Error fatal, no se puedo cargar el archivo de configuracion de vistas." + serEx);
		}
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
		int metodo = Integer.parseInt(request.getParameter("method"));
		RequestDispatcher dispatcher = null;
		try {
			switch (metodo) {
			case INIT_MAIL:
				dispatcher = request.getRequestDispatcher(viewConfig.getProperty("mail.pagina.form"));
				dispatcher.forward(request, response);
				break;
			case SEND_MAIL:
				log.info("--------------------{SEND_MAIL}---------------------");
				mailService.sendMail(getMailFromRequest(request));
				dispatcher = request.getRequestDispatcher(viewConfig.getProperty("mail.pagina.form"));
				dispatcher.forward(request, response);
				break;
			case VALIDAR_REGS:

				break;
			}
		} catch (Exception e) {
			request.setAttribute("ex", e);
            dispatcher = request.getRequestDispatcher(viewConfig.getProperty("error"));
            dispatcher.forward(request, response);
			System.out.println("Error en LoginController " + e);
			e.printStackTrace();
		} finally {
            out.close();
        }
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
    private MailVo getMailFromRequest(HttpServletRequest request) {
    	MailVo mail = new MailVo();
            if (StringUtils.isNotBlank(request.getParameter(fieldConfig.getProperty("form.mail.de")))) {
                mail.setMailDe(String.valueOf(request.getParameter(fieldConfig.getProperty("form.mail.de"))));
            } 
            if (StringUtils.isNotBlank(request.getParameter(fieldConfig.getProperty("form.mail.para")))) {
                mail.setMailPara(String.valueOf(request.getParameter(fieldConfig.getProperty("form.mail.para"))));
            } 
            if (StringUtils.isNotBlank(request.getParameter(fieldConfig.getProperty("form.mail.ccp")))) {
                mail.setMailCcp(String.valueOf(request.getParameter(fieldConfig.getProperty("form.mail.ccp"))));
            } 
            if (StringUtils.isNotBlank(request.getParameter(fieldConfig.getProperty("form.mail.asunto")))) {
                mail.setMailAsunto(String.valueOf(request.getParameter(fieldConfig.getProperty("form.mail.asunto"))));
            } 
            if (StringUtils.isNotBlank(request.getParameter(fieldConfig.getProperty("form.mail.contenido")))) {
                mail.setMailContenido(String.valueOf(request.getParameter(fieldConfig.getProperty("form.mail.contenido"))));
            } 
            
        return mail;
    }
}
